package cn.innosmart.aq.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import cn.innosmart.aq.R;
import cn.innosmart.aq.bean.DeviceBean;
import cn.innosmart.aq.bean.SwitchHistoryData;
import cn.innosmart.aq.util.DeviceConstant;

/**
 * Created by David on 2015/9/2.
 */
public class HistorySwitchListAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater mInflater;
    private ArrayList<SwitchHistoryData> items;
    private DeviceBean deviceBean;

    /**
     * 插座类型（无图表）历史数据列表
     *
     * @param deviceBean 要显示的设备
     * @param context
     */
    public HistorySwitchListAdapter(DeviceBean deviceBean, Context context) {
        this.context = context;
        this.deviceBean = deviceBean;
        this.mInflater = LayoutInflater.from(context);
        items = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.switch_history_item, null);
            holder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tvTime = (TextView) convertView.findViewById(R.id.tv_time);
            holder.tvValue = (TextView) convertView.findViewById(R.id.tv_value);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvName.setText(deviceBean.getName() + ":");
        SwitchHistoryData switchHistoryData = items.get(position);
        String time = switchHistoryData.getTimeStamp();
        long timestamp = Long.valueOf(time);
        DateFormat dateFormat = null;
        dateFormat = new SimpleDateFormat("MM-dd HH:mm:ss", Locale.CHINA);
        String timer = dateFormat.format(new Date(timestamp * 1000));
        holder.tvTime.setText(timer);
        double value = switchHistoryData.getValue();
        if (DeviceConstant.FUNCTYPE_SWITCH == deviceBean.getFunctype()) {
            if (value == 255) {
                holder.tvValue.setText(context.getString(R.string.switch_open));
            } else if (value == 0) {
                holder.tvValue.setText(context.getString(R.string.switch_off));
            } else {
                holder.tvValue.setText(context.getString(R.string.listview_temperature_unknown_value));
            }
        } else if (DeviceConstant.FUNCTYPE_WATERLEVER == deviceBean.getFunctype()) {
            if (value == 0) {
                holder.tvValue.setText(context.getString(R.string.water_lever_value_normal));
            } else if (value == 1) {
                holder.tvValue.setText(context.getString(R.string.water_lever_value_low));
            } else if (value == 2) {
                holder.tvValue.setText(context.getString(R.string.water_lever_value_high));
            } else {
                holder.tvValue.setText(context.getString(R.string.listview_temperature_unknown_value));
            }
        } else if (DeviceConstant.FUNCTYPE_FEEDER == deviceBean.getFunctype()) {
            if (value == 255) {
                holder.tvValue.setText(context.getString(R.string.feed));
            } else if (value == 0) {

            } else {
                holder.tvValue.setText(context.getString(R.string.listview_temperature_unknown_value));
            }
        }
        return convertView;
    }

    /**
     * 刷新数据
     * @param items
     */
    public void refreshData(ArrayList<SwitchHistoryData> items) {
        this.items = items;
        notifyDataSetChanged();
    }

    static class ViewHolder {
        TextView tvName;
        TextView tvTime;
        TextView tvValue;
        TextView tvNoitem;
    }
}
